using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Material.Entity;
using Material.Object;
using System.Configuration;

namespace Material.App
{
    public partial class FrmUser : Form
    {
        private string connStr = ConfigurationManager.ConnectionStrings["dbconnection"].ToString();
        private TblUser tblUser;
        private TblUserEntity tblUserEntity;
        private string stateInput = string.Empty;
        //private int IdSatuan = 0;

        public FrmUser()
        {
            InitializeComponent();

            ClearTextbox();
            EnabledTextboxs(false, false,false);
            EnabledButtons(true, false, false, true);
            BindGridData();

            dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
            dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
            dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;

            dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Green;
            dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Yellow;

            //dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            //dataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //dataGridView1.AllowUserToResizeColumns = false;
        }

        /// <summary>
        /// Kondisi untuk Disable dan Enable Kontrol [Textbox dan Button]
        /// </summary>
        /// <param name="state">boolean True dan False</param>
        private void EnabledTextboxs(bool bltxtuserName, bool bltxtPassword
            ,bool bltxtNama)
        {
            txtUserName.Enabled = bltxtuserName;
            txtPassword.Enabled = bltxtPassword;
            txtNama.Enabled = bltxtNama;
            //txtPassword.Enabled = bltxtKeterangan;

        }

        /// <summary>
        /// Kondisi Untuk Disable dan Enable Button
        /// </summary>
        /// <param name="blbtnNew"></param>
        /// <param name="blbtnCancel"></param>
        /// <param name="blbtnSave"></param>
        /// <param name="blbtnPrint"></param>
        private void EnabledButtons(bool blbtnNew, bool blbtnCancel, bool blbtnSave, bool blbtnPrint)
        {
            btnNew.Enabled = blbtnNew;
            btnCancel.Enabled = blbtnCancel;
            btnSave.Enabled = blbtnSave;
            btnPrint.Enabled = blbtnPrint;

        }

        /// <summary>
        /// Clear Semua TextBox
        /// </summary>
        private void ClearTextbox()
        {
            txtUserName.Text = string.Empty;
            txtPassword.Text = string.Empty;
            txtNama.Text = string.Empty;
        }

      

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Kolom User Name Harus Diisi..", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtUserName.Focus();
                return;
            }
            else if (txtNama.Text.Trim().Length == 0)
            {
                MessageBox.Show("Kolom Nama Harus Diisi..", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtNama.Focus();
                return;
            }
            else if (txtPassword.Text.Trim().Length == 0)
            {
                MessageBox.Show("Kolom Password Harus Diisi..", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPassword.Focus();
                return;
            }
            else
            {

                tblUser = new TblUser();
                tblUserEntity = new TblUserEntity(connStr);
                int i = 0;

                tblUser.UserID = txtUserName.Text;
                tblUser.Password = txtPassword.Text;
                tblUser.Nama = txtNama.Text;
                tblUser.TipeUser = rbAdmin.Checked == true ? "1" : "0";
                if (stateInput == "Add")
                {
                    tblUser.CreatedDate = DateTime.Now;
                    tblUser.CreatedBy = FrmLogin.UserID;
                    tblUser.ModifiedDate = DateTime.Now;
                    tblUser.ModifiedBy = FrmLogin.UserID;
                    i = tblUserEntity.Insert(tblUser);
                }
                else
                {

                    tblUser.ModifiedBy = FrmLogin.UserID;
                    tblUser.ModifiedDate = DateTime.Now;
                    i = tblUserEntity.Update(tblUser);
                }
                if (i != -1)
                {
                    ClearTextbox();
                    EnabledTextboxs(false, false, false);
                    EnabledButtons(true, false, false, true);
                    BindGridData();
                    MessageBox.Show("Simpan data berhasil", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                    MessageBox.Show("Simpan data tidak berhasil", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }



        }

        private void BindGridData()
        {
            tblUserEntity = new TblUserEntity(connStr);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = tblUserEntity.GetDataAll();


        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            EnabledTextboxs(true, true,true);
            EnabledButtons(false, true, true, false);
            txtUserName.Focus();
            stateInput = "Add";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            EnabledTextboxs(false, false,false);
            EnabledButtons(true, false, false, true);
            ClearTextbox();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if ((e.RowIndex > -1) && (e.ColumnIndex > -1))
            {

                switch (e.ColumnIndex)
                {
                    case 3:
                        ClearTextbox();
                        EnabledTextboxs(true, true,true);
                        EnabledButtons(false, true, true, false);
                        tblUserEntity = new TblUserEntity(connStr);
                        tblUser = new TblUser();
                        tblUser.UserID = (dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
                        tblUser = tblUserEntity.GetDataByID(tblUser);
                        txtUserName.Text = tblUser.UserID;
                        txtNama.Text = tblUser.Nama;
                        txtPassword.Text = tblUser.Password;

                        if (tblUser.TipeUser == "User")
                        {
                            rbUser.Checked = true;
                            rbAdmin.Checked = false;
                        }
                        else
                        {
                            rbAdmin.Checked = true;
                            rbUser.Checked = false;
                        }
                        stateInput = "Edit";
                        break;

                    case 4:
                        DialogResult dr = MessageBox.Show("Data akan dihapus?", "Pesan", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (dr == DialogResult.Yes)
                        {
                            tblUserEntity = new TblUserEntity(connStr);
                            tblUser = new TblUser();
                            tblUser.UserID = (dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
                            int i = tblUserEntity.Delete(tblUser);
                            BindGridData();
                        }
                        break;
                }

            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            FrmRptUser frm = new FrmRptUser();
            frm.ShowDialog();
            }


        
    }
}